# _*_ coding:utf-8 _*_
# @Time  : 2023.08.13
# @Author: zizlee
from fastapi import APIRouter, Query
from response import APIResponse
from db_utils import BaseDatabaseConnection

variety_api = APIRouter()


class VarietyApi:
    def __init__(self, scope: str = None):
        self.scope = scope

    def get_scope_variety(self):
        sql = "SELECT id,code,name,scope FROM ruizhi_variety"
        prm = None
        if self.scope:
            sql += " WHERE FIND_IN_SET(%s,scope)"
            prm = [self.scope]
        sql += " ORDER BY `code`;"
        with BaseDatabaseConnection() as conn:
            conn.cursor.execute(sql, prm)
            ret = conn.cursor.fetchall()
        return APIResponse.success({'data': ret})


@variety_api.get('/')
def get_scope_variety(scope: str = Query(None)):
    return VarietyApi(scope=scope).get_scope_variety()

